<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/5/5
 * Time: 16:40
 */

namespace Common\Lib\TagLib;


use Think\Template\TagLib;

class TagLibPlugin extends TagLib
{
    protected $tags = array(
        'plugintpl' => array("attr" => "file", "close" => 0),
    );
    /**
     * 模板包含标签
     * 格式
     * <tc_include file=""/>
     * @staticvar array $_tc_include_templateParseCache
     * @param type $tag 属性数据
     * @param type $content 标签内容
     * @return array
     */
    public function _plugintpl($tag, $content) {

        static $_tc_include_templateParseCache = array();
        $file = str_replace(":", "/", $tag['file']);
        $cacheIterateId = md5($file . $content);
        if (isset($_tc_include_templateParseCache[$cacheIterateId])) {
            return $_tc_include_templateParseCache[$cacheIterateId];
        }
        $path = PLUGIN_PATH.'/'.$_GET['_plugin']."/View/".$file;
        $TemplatePath = sp_add_template_file_suffix($path) ;
        //判断模板是否存在
        if (!file_exists_case($TemplatePath)) {
            return false;
        }
        //读取内容
        $tmplContent = file_get_contents($TemplatePath);
        //解析模板内容
        $parseStr = $this->tpl->parse($tmplContent);
        $_tc_include_templateParseCache[$cacheIterateId] = $parseStr;
        return $_tc_include_templateParseCache[$cacheIterateId];
    }
}